//
//  ZZP_HomeViewController.m
//  SlowPlay
//
//  Created by dllo on 15/10/19.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZZP_HomeViewController.h"
#import "ZZPMusicModel.h"
#import "ZZPMusicCell.h"
#import <MJRefresh.h>
#import "AppDelegate.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGTH [UIScreen mainScreen].bounds.size.height

@interface ZZP_HomeViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
{
    NSInteger _tempTable;
    NSInteger _countUpdate;
}
@property (nonatomic, strong) UIScrollView *scrollerView;
@property (nonatomic, strong) UICollectionView *collectionViewMusic;
@property (nonatomic, strong) UITableView *tableViewRaido;
@property (nonatomic, strong) UISegmentedControl *sem;
@property (nonatomic, strong) NSMutableArray *musicArr;
@property (nonatomic, strong) NSMutableArray *radioArr;

@end

static NSString *const cellIndMusic = @"music";
static NSString *const cellIndRadio = @"radio";
@implementation ZZP_HomeViewController
#pragma mark - 初始化
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.musicArr = [NSMutableArray array];
        self.radioArr = [NSMutableArray array];
    }
    return self;
}
#pragma mark - 创建头部
- (void)createNavBar
{
    self.sem = [[UISegmentedControl alloc] initWithItems:@[@"音乐", @"电台"]];
    
    _sem.selectedSegmentIndex = 0;
    _sem.frame = CGRectMake(0, -10, 120, 30);
    [self.sem addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = self.sem;
}


#warning -------
- (void)drawAction
{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    YRSideViewController *sideViewController = [delegate muSideViewController];
    
    sideViewController.animationDuration = 0.5;
    [sideViewController setNeedSwipeShowMenu:YES];
    [sideViewController showLeftViewController:true];
}


#pragma mark - 点击segment 实现tableView的跳转
- (void)segmentedAction:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0) {
        
        NSLog(@"music");
        [self.scrollerView setContentOffset:CGPointMake(0, 0) animated:YES];
        [self.collectionViewMusic reloadData];
    } else {
        
        NSLog(@"radio");
        
        if (_tempTable == 0) {
            
            [self.scrollerView addSubview:self.tableViewRaido];
            _tempTable++;
        }
        [self.scrollerView setContentOffset:CGPointMake(WIDTH, 0) animated:YES];
        [self.tableViewRaido reloadData];
    }
}

- (UIScrollView *)scrollerView
{
    if (_scrollerView != nil) {
        
        return _scrollerView;
    } else {
        
        self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGTH - 60)];
        self.scrollerView.contentSize = CGSizeMake(2 * WIDTH, 0);
        self.scrollerView.pagingEnabled = YES;
        self.scrollerView.delegate = self;
        return _scrollerView;
    }
    
    
}
- (UICollectionView *)collectionViewMusic
{
    if (_collectionViewMusic != nil) {
        
        return _collectionViewMusic;
    } else {
        
        UICollectionViewFlowLayout *flaw = [[UICollectionViewFlowLayout alloc] init];
        flaw.minimumLineSpacing = 10;
        flaw.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
        
        self.collectionViewMusic = [[UICollectionView alloc] initWithFrame:CGRectMake(0,64, WIDTH , HEIGTH - 100) collectionViewLayout:flaw];
        self.collectionViewMusic.backgroundColor = [UIColor whiteColor];
        self.collectionViewMusic.delegate = self;
        self.collectionViewMusic.dataSource = self;
        
        
        [self.collectionViewMusic registerClass:[ZZPMusicCell class] forCellWithReuseIdentifier:cellIndMusic];
        return _collectionViewMusic;
    }
}

#pragma mark - 上拉加载
- (void)updateMusicData
{
    _countUpdate++;
    NSString *tempBefore = @"http://so.ard.iyyin.com/s/songlist?q=tag:%E6%9C%80%E7%83%AD&page=";
    NSString *tempFinish = [tempBefore stringByAppendingString:[NSString stringWithFormat:@"%ld", _countUpdate]];
    
    NSString *temp = [tempFinish stringByAppendingString:@"&size=10&app=ttpod&v=v8.0.1.2015091618&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=9.0.2&active=1&net=2&openudid=0e60f9ab38de1baeca117f7972cd7591b840dd02&idfa=84063A0D-E652-44CD-86DE-52D73D2D28AA&utdid=VgIwxBZX38gDAJbMSbp3R55y&alf=201200&bundle_id=com.ttpod.music&latitude=38.88272909238047&longtitude=121.5393703093171"];
    [NetWork netWorkGETRequestWithURLAndNoMub:temp Paramater:nil ResultBlock:^(id result) {
        
        // 若存在数据停止刷新
        if (result != nil) {
            
            [_collectionViewMusic.footer endRefreshing];
        }
        // 若没有数据就提示
        if (result == nil) {
            [_collectionViewMusic.footer endRefreshing];
            [_collectionViewMusic.footer noticeNoMoreData];
            
        }
        NSArray *arrayTemp = [(NSDictionary *)result objectForKey:@"data"];
        
        // 遍历数组就其存到数据源中
        for (NSDictionary *dic in arrayTemp) {
            
            ZZPMusicModel *model = [[ZZPMusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.musicArr addObject:model];
            
        }
        if (self.musicArr.count > 0) {
            
            [_collectionViewMusic reloadData];
        }
    }];
}
#pragma mark - 刷新数据
- (void)refreash
{
    
    self.collectionViewMusic.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        [self updateMusicData];
    }];
}
#pragma mark - collectionView 的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGSize size = CGSizeMake((WIDTH - 20) / 2, (WIDTH - 20) /2 + 10);
    return size;
}
#pragma mark - collection的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZZPMusicCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIndMusic forIndexPath:indexPath];
    
    NSLog(@"%ld", self.musicArr.count);
    
    cell.model = [self.musicArr objectAtIndex:indexPath.item];
    
    return cell;
}
#pragma mark - 返回个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.musicArr.count > 0) {
        
        return self.musicArr.count;
    } else {
        
        return 0;
    }
    
}

- (UITableView *)tableViewRaido
{
    if (_tableViewRaido != nil) {
        
        return _tableViewRaido;
    } else {
        
        self.tableViewRaido = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH, 10, WIDTH / 2, HEIGTH - 74) style:UITableViewStylePlain];
        
        self.tableViewRaido.delegate = self;
        self.tableViewRaido.dataSource = self;
        [self.tableViewRaido registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIndRadio];
        return _tableViewRaido;
    }
}

#pragma mark - 创建视图
- (void)createView
{
    [self.view addSubview:self.scrollerView];
    if (self.scrollerView.contentOffset.x == 0) {
        
        [self.scrollerView addSubview:self.collectionViewMusic];
        [self refreash];
    }
    
}


#pragma mark - 滚动视图
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x == 0) {
        
        
        self.sem.selectedSegmentIndex = 0;
        [_collectionViewMusic reloadData];
        
    } else {
        
        self.sem.selectedSegmentIndex = 1;
        if (_tempTable == 0) {
            
            [self.scrollerView addSubview:self.tableViewRaido];
            _tempTable++;
        }
        
        [_tableViewRaido reloadData];
    }
}
#pragma mark -获取音乐数据
- (void)getDataMusic
{
    
    [NetWork netWorkGETRequestWithURLAndNoMub:MusicPage Paramater:nil ResultBlock:^(id result) {
        
        if (result != nil) {
            
            [self.collectionViewMusic.header endRefreshing];
        }
        NSMutableArray *arrMusic = [(NSDictionary *)result objectForKey:@"data"];
        
        _musicArr = [ZZPMusicModel baseModelArrayWithArray:arrMusic];
        
        if (self.musicArr.count > 0) {
            
            [_collectionViewMusic reloadData];
        }
    }];
}
#pragma mark - 获取电台音乐
- (void)getDataRadio
{
    
}

#pragma mark - 每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.radioArr.count > 0) {
        
        return 20;
    } else {
        
        return 0;
    }
    
}
#pragma mark - tableView 重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndRadio];
    cell.textLabel.text = @"hahah";
    return cell;
}
#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavBar];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_menu_icon@2x"] style:UIBarButtonItemStyleDone target:self action:@selector(drawAction)];
    
    
    _tempTable = 0;
    _countUpdate = 1;
    [self createView];
    [self getDataMusic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
